package com.dream.oproject.base.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;

import com.dream.oproject.base.dao.IBaseDao;
import com.dream.oproject.base.model.IdEntity;
import com.dream.oproject.page.PageConst;
import com.dream.oproject.page.PageInfo;
import com.dream.oproject.utils.reflection.ReflectionUtils;

@SuppressWarnings("unchecked")
public class BaseDao<T extends IdEntity, PK extends Serializable>  
	extends SqlSessionDaoSupport implements IBaseDao<T, PK> {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	protected Class<T> entityClass;
	
	public String getNameSpace(){
		return this.entityClass.getName();
	}
	
	public BaseDao() {
		this.entityClass = ReflectionUtils.getSuperClassGenricType(getClass());
	}

	public int delete(PK id) throws DataAccessException {
		return getSqlSession().delete(getNameSpace() + ".delete", id);
	}

	public T get(PK id) throws DataAccessException {
		return (T) getSqlSession().selectOne(getNameSpace() + ".getById", id);
	}

	public int save(final T entity) throws DataAccessException {
		int i = 0;
		if (entity.getOid() != null)
			i = getSqlSession().update(getNameSpace() + ".update", entity);
		else
			i =getSqlSession().insert(getNameSpace() + ".insert", entity);
		return i;
	}

	public PageInfo getObjectListByPage(PageInfo pageInfo) throws DataAccessException{
		Map<String,Object> map = setSqlParamByPageInfo(pageInfo);
		List<T> list = new ArrayList();
		int pageNo = pageInfo.getPageNo();
		Integer count = new Integer(0);
//		if(pageNo == 1){
			count = (Integer)getSqlSession().selectOne(getNameSpace() + ".totalPage", map);
//		}else{
//			count = pageInfo.getTotalPage();
//		}
		pageInfo.setTotalData(count);
		list = getSqlSession().selectList(getNameSpace() + ".getObjectList", map);
		pageInfo.setDatas(list);
		return pageInfo;
	}
	
	protected Map<String, Object> setSqlParamByPageInfo(PageInfo pageInfo) {
		Map<String,Object> map = pageInfo.getFilter();
		if(map == null || map.size() == 0){
			map = new HashMap();
		}
		int pageSize = pageInfo.getPageSize();
		if (pageSize <=0 ) {
			pageSize = PageConst.DEF_PAGE_SIZE;
		}
		map.put("pageSize",pageSize);
		
		int pageNo = pageInfo.getPageNo();
		Integer startIdx = 1;
		if (pageNo >0) {
			startIdx =((pageNo - 1) * pageSize);
		}
		map.put("startIndex", startIdx);
		map.put("endIndex", startIdx+pageSize);
		
		if(StringUtils.isNotEmpty(pageInfo.getSortName())){
			map.put("sortName", pageInfo.getSortName());
			map.put("sortOrder", pageInfo.getSortOrder());
		}
		
		if(pageInfo.getFilter() != null && pageInfo.getFilter().size()>0){
			map.putAll(pageInfo.getFilter());
		}
		return map;
		
	}

}